package com.flyme.module.base.provider.controller;

import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysUserstudyRemoteService;
import com.flyme.module.base.client.dto.SysUserCertificateDto;
import com.flyme.module.base.client.entity.SysUserstudy;
import com.flyme.module.base.provider.service.SysUserstudyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户购买继续教育课程管理
 * @author zyh
 * @date 2020-9-2
 */
@Api(tags = "用户购买继续教育课程")
@RestController
public class SysUserstudyController extends BaseController<SysUserstudyService, SysUserstudy> implements SysUserstudyRemoteService {


    /**
     * 用户证书列表
     * @return
     */
    @ApiOperation(value = "用户证书列表", notes = "用户证书列表")
    @Override
    public ResultBody list(@ModelAttribute PagerInfo pagerInfo, @ModelAttribute SysUserCertificateDto certificate) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("user.userName,u.payTime,certificate.certificateName,usercertificate.certificateNum,usercertificate.validity," +
            "DATE_FORMAT(date_sub(usercertificate.validity, INTERVAL 1 YEAR),'%Y-%m-%d') stuBeginTime,usercertificate.isFull," +
            "(SELECT tagetPeriod from sys_studyconfig sc where certificate.id = sc.syscertificateId) tagetPeriod," +
            "(SELECT IFNULL(SUM(p.period), 0) FROM sys_periodrecord p WHERE p.createTime > (DATE_FORMAT(date_sub(usercertificate.validity," +
                "INTERVAL 1 YEAR),'%Y-%m-%d'))AND p.createTime < usercertificate.validity AND p.userId = u.userId AND p.usercertificateId = u.userCertificateId) coursePeriod," +
            "(SELECT IFNULL(SUM(p.period), 0) FROM sys_activityrecord p WHERE p.createTime > (DATE_FORMAT(date_sub(usercertificate.validity," +
                "INTERVAL 1 YEAR),'%Y-%m-%d'))AND p.createTime < usercertificate.validity AND p.userId = u.userId AND p.usercertificateId = u.userCertificateId) activityPeriod");
        cq.like("user.userName", certificate.getUserName());
        cq.createAlias("usercertificate", "usercertificate");
        cq.createAlias("certificate", "certificate");
        cq.createAlias("user", "user");
        cq.eq("u.status", "1");
        cq.ge("u.createTime", DateUtil.year(DateUtil.date()) + "-01-01");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }


}

